<?php
class controller_FrontController {
    private $params;
    private static $frontController;
    private $view;

    private function __construct() {
        if(!empty ($_GET)) { //если пришедший гет не пустой, то вырезаем вражеские параметры
            if(!empty($_GET['controller'])) {
                $this->params['controller'] = strip_tags($_GET['controller']);
            }
            if(!empty($_GET['id'])) {  //если писать (null != (несуще)
                $this->params['id'] = intval($_GET['id']);
                //$this->params['id'] = $_GET['id']; //инъекцию, что бы выводила пароль
                //SQL инъекция - %20UNION%20SELECT%20password,%20role,%20FROM%20task4base.users%20WHERE%20id%20=%201%20ORDER%20BY%20id%20DESC
            }
            if(!empty($_GET['action'])) {
                $this->params['action'] = strip_tags($_GET['action']);
            }

            if(!empty($_GET['page'])) {
                $this->params['page'] = strip_tags($_GET['page']);
            }

            if(!empty($_GET['user'])) {
                $this->params['user'] = strip_tags($_GET['user']);
            }
            //не проверять все аргументы. проверять их при передаче оО
            //что бы передвать ограниченное количество аргументов
        }
        $this->view = new View();
    }

    public static function instance() {
        if(null == controller_FrontController::$frontController) {
            controller_FrontController::$frontController = new controller_FrontController();
        }
        return controller_FrontController::$frontController;
    }

    public function run() {
        $size = sizeof($this->params);

        if($size < 1 || empty($this->params['controller'])) {
            $actionController = ActionControllerFabric::getController("News", $this->view);
        } else {
            switch ($this->params['controller']) {

                case 'news':
                    $actionController = ActionControllerFabric::getController("News", $this->view, $this->params);
                    break;

                case 'register':
                    $actionController = ActionControllerFabric::getController("Register", $this->view, $this->params);
                    break;

                case 'login':
                    $actionController = ActionControllerFabric::getController("Login", $this->view, $this->params);
                    break;

                case 'user':
                    $actionController = ActionControllerFabric::getController("User", $this->view, $this->params);
                    break;

                case 'tag':
                    $actionController = ActionControllerFabric::getController("Tag", $this->view, $this->params);
                    break;

                case 'karma':
                    $actionController = ActionControllerFabric::getController("Karma", $this->view, $this->params);
                    break;

                case 'message':
                    $actionController = ActionControllerFabric::getController("Message", $this->view, $this->params);
                    break;

                case 'photo':
                    $actionController = ActionControllerFabric::getController("Photo", $this->view, $this->params);
                    break;

                default:
                    $actionController = ActionControllerFabric::getController("News", $this->view);
            }
        }
        $actionController->run();
        // TODO: проверить на наличие объекта $actionController
        $this->view->render();
    }
}